package com.cloud.system.api.provider;

import com.cloud.system.api.bean.PatrolCriteriaBean;
import org.apache.commons.lang.StringUtils;
import org.apache.ibatis.jdbc.SQL;

public class PatrolPageProvider {
    private static final String LIST_PAGE_SELECT = "t1.human_id,t1.human_name,t1.unit_name,CASE WHEN lawcard_flag=1 THEN '是' ELSE '否' end as lawcard_flag,CASE WHEN occup_flag=1 THEN '是' ELSE '否' end as occup_flag , human_nature, position_level",
            LIST_PAGE_FOMR = " human_info t1 ";

    public String listPage(PatrolCriteriaBean bean) {
        SQL sql = new SQL().SELECT(LIST_PAGE_SELECT).FROM(LIST_PAGE_FOMR);
        sql.WHERE("t1.delete_flag=0");

        // 区域权限过滤
        if (StringUtils.isNotEmpty(bean.getUnitCode())) {
            bean.setUnitCode(bean.getUnitCode() + "%");
            sql.WHERE("t1.unit_code like '" + bean.getUnitCode() + "'");
        }
        // 领域权限过滤
        if (StringUtils.isNotEmpty(bean.getDomains())) {
            sql.WHERE("t1.unit_id in ( select unit_id from unit_domain where sub_domain_type_id in (" + bean.getDomains()
                    + ") ) ");
        }
        if (StringUtils.isNotEmpty(bean.getUnitName())) {
            bean.setUnitName("%" + bean.getUnitName() + "%");
            sql.WHERE("t1.unit_name like #{unitName}");
        }
        if (StringUtils.isNotEmpty(bean.getHumanName())) {
            bean.setHumanName("%" + bean.getHumanName() + "%");
            sql.WHERE("t1.human_name like #{humanName}");
        }
        if (StringUtils.isNotEmpty(bean.getLawcardFlag())) {
            if (bean.getLawcardFlag().equals("01")) {
                sql.WHERE("t1.lawcard_flag = 1");
            } else {
                sql.WHERE("t1.lawcard_flag =0");
            }
        }

        if (StringUtils.isNotEmpty(bean.getLawyerFlag())) {
            if (bean.getLawyerFlag().equals("01")) {
                sql.WHERE("t1.lawyer_flag = 1");
            } else {
                sql.WHERE("t1.lawyer_flag =0");
            }
        }

        if (StringUtils.isNotEmpty(bean.getOccupFlag())) {
            if (bean.getOccupFlag().equals("01")) {
                sql.WHERE("t1.occup_flag = 1");
            } else {
                sql.WHERE("t1.occup_flag =0");
            }
        }

        if (StringUtils.isNotEmpty(bean.getGender())) {
            if (bean.getGender().equals("01")) {
                sql.WHERE("t1.gender = 1");
            } else {
                sql.WHERE("t1.gender = 2");
            }
        }


        if (StringUtils.isNotEmpty(bean.getConfirmFlag())) {
            if (bean.getConfirmFlag().equals("01")) {
                sql.WHERE("t1.is_confirm = 1");
            } else {
                sql.WHERE("(t1.is_confirm =0)");
            }
        }

        if (bean.getHumanNature() != null) {
            sql.WHERE("t1.human_nature = #{humanNature}");
        }

        if (bean.getPositionLevel() != null) {
            sql.WHERE("t1.position_level = #{positionLevel}");
        }

        if (bean.getHumanType() != null) {
            if (bean.getHumanType().equals("01")) {
                sql.WHERE("t1.is_law_human = 1");
            }else {
                sql.WHERE("t1.is_law_human = 0");
            }
            sql.WHERE("t1.human_type != '00'");
        }

        return sql.toString();
    }

    public String noDomainsListPage(PatrolCriteriaBean bean) {
        SQL sql = new SQL().SELECT(LIST_PAGE_SELECT).FROM(LIST_PAGE_FOMR);
        sql.WHERE("unit_id = " + bean.getUnitId());
        if (StringUtils.isNotEmpty(bean.getUnitName())) {
            bean.setUnitName("%" + bean.getUnitName() + "%");
            sql.WHERE("t1.unit_name like #{unitName}");
        }
        if (StringUtils.isNotEmpty(bean.getHumanName())) {
            bean.setHumanName("%" + bean.getHumanName() + "%");
            sql.WHERE("t1.human_name like #{humanName}");
        }
        if (StringUtils.isNotEmpty(bean.getLawcardFlag())) {
            if (bean.getLawcardFlag().equals("01")) {
                sql.WHERE("t1.lawcard_flag = 1");
            } else {
                sql.WHERE("t1.lawcard_flag =0");
            }
        }

        if (StringUtils.isNotEmpty(bean.getLawyerFlag())) {
            if (bean.getLawyerFlag().equals("01")) {
                sql.WHERE("t1.lawyer_flag = 1");
            } else {
                sql.WHERE("t1.lawyer_flag =0");
            }
        }

        if (StringUtils.isNotEmpty(bean.getOccupFlag())) {
            if (bean.getOccupFlag().equals("01")) {
                sql.WHERE("t1.occup_flag = 1");
            } else {
                sql.WHERE("t1.occup_flag =0");
            }
        }

        if (StringUtils.isNotEmpty(bean.getGender())) {
            if (bean.getGender().equals("01")) {
                sql.WHERE("t1.gender = 1");
            } else {
                sql.WHERE("t1.gender = 2");
            }
        }


        if (StringUtils.isNotEmpty(bean.getConfirmFlag())) {
            if (bean.getConfirmFlag().equals("01")) {
                sql.WHERE("t1.is_confirm = 1");
            } else {
                sql.WHERE("(t1.is_confirm =0)");
            }
        }

        if (bean.getHumanNature() != null) {
            sql.WHERE("t1.human_nature = #{humanNature}");
        }

        if (bean.getPositionLevel() != null) {
            sql.WHERE("t1.position_level = #{positionLevel}");
        }
        if (bean.getHumanType() != null) {
            sql.WHERE("t1.human_type = #{humanType}");
        }
        System.out.println(sql.toString());
        return sql.toString();
    }
}
